package com.paomi.client.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.paomi.client.R;
import com.paomi.client.fragment.mine.model.SignDataBean;
import com.paomi.client.view.calendarview.Calendar;
import com.paomi.client.view.calendarview.CalendarView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2018/10/31
 * Describe:签到弹出框
 * com.paomi.client.view
 */
public class SignInDialog extends Dialog implements
        CalendarView.OnCalendarInterceptListener,
        CalendarView.OnCalendarRangeSelectListener{
    private static Context mcontext;
    private View layout;
    private SignInDialog dialog;
    public Typeface iconfont;
    CalendarView mCalendarView;
    public SignInDialog(Context context) {
        super(context);
        iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
        this.mcontext=context;
    }
//    private static final SignInDialog mInstance = new SignInDialog(mcontext);
//
//    public static SignInDialog get() {
//        return mInstance;
//    }
    public SignInDialog(Context context, int theme) {
        super(context, theme);
    }
    public SignInDialog creatDialog(int year, int month, List<SignDataBean.SignDataJson> signList){
        //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
        dialog = new SignInDialog(mcontext, R.style.theme_dialog);
        LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.dilog_calendar_sign, null);
        dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity( Gravity.CENTER);
        mCalendarView = dialog.findViewById(R.id.calendarView);
        mCalendarView.setOnCalendarRangeSelectListener(this);

        //设置日期拦截事件，当前有效
        mCalendarView.setOnCalendarInterceptListener(this);
        Map<String, Calendar> map = new HashMap<>();
        if(signList.size()>0){
            for(int i=0;i<signList.size();i++){
                int day=signList.get(i).getDay();
                map.put(getSchemeCalendar(year, month,day ).toString(),
                        getSchemeCalendar(year, month, day));
            }
        }
        mCalendarView.setSchemeDate(map);
        TextView date_get=dialog.findViewById(R.id.date_get);
        date_get.setText(String.format(mcontext.getResources().getString(R.string.sign_get), signList.size()));
        TextView textView=dialog.findViewById(R.id.tv_close);
        changeColor(textView,R.color.white,mcontext.getResources().getString(R.string.sign_close));
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        return dialog;
    }
    private void changeColor(TextView iv, int color, String iv_icon) {
        iv.setText(iv_icon);
        iv.setTextColor(mcontext.getResources().getColor(color));
        iv.setTypeface(iconfont);
    }
    private Calendar getSchemeCalendar(int year, int month, int day) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        return calendar;
    }

    /**
     * 屏蔽某些不可点击的日期，可根据自己的业务自行修改
     * 如 calendar > 2018年1月1日 && calendar <= 2020年12月31日
     *
     * @param calendar calendar
     * @return 是否屏蔽某些不可点击的日期，MonthView和WeekView有类似的API可调用
     */
    @Override
    public boolean onCalendarIntercept(Calendar calendar) {
        //Log.e("onCalendarIntercept", calendar.toString());
//        int day = calendar.getDay();
//        return day == 1 || day == 3 || day == 6 || day == 11 ||
//                day == 12 || day == 15 || day == 20 || day == 26;
        return false;
    }

    @Override
    public void onCalendarInterceptClick(Calendar calendar, boolean isClick) {
        Toast.makeText(mcontext,
                calendar.toString() + (isClick ? "拦截不可点击" : "拦截滚动到无效日期"),
                Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onCalendarSelectOutOfRange(Calendar calendar) {
        // TODO: 2018/9/13 超出范围提示
    }

    @Override
    public void onSelectOutOfRange(Calendar calendar, boolean isOutOfMinRange) {
        Toast.makeText(mcontext,
                calendar.toString() + (isOutOfMinRange ? "小于最小选择范围" : "超过最大选择范围"),
                Toast.LENGTH_SHORT).show();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onCalendarRangeSelect(Calendar calendar, boolean isEnd) {
        if (!isEnd) {

        } else {
        }
    }

    private static final String[] WEEK = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
}
